package cn.com.twoke.easyproject.modules.system.controller;

import cn.com.twoke.easyproject.common.exception.BusinessException;
import cn.com.twoke.easyproject.common.response.R;
import cn.com.twoke.easyproject.common.response.Result;
import cn.com.twoke.easyproject.common.utils.IdUtils;
import cn.com.twoke.easyproject.config.version.ApiVersion;
import cn.com.twoke.easyproject.modules.system.converter.SysUserConverter;
import cn.com.twoke.easyproject.modules.system.domain.SysUser;
import cn.com.twoke.easyproject.modules.system.request.SaveOrUpdateSysUserRequest;
import cn.com.twoke.easyproject.modules.system.response.SysUserResponse;
import cn.com.twoke.easyproject.modules.system.service.SysUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (SysUser)表控制层
 *
 * @author TwoKe
 * @since 2025-06-07 03:42:34
 */
@RestController
@RequestMapping("/system/user")
@RequiredArgsConstructor
@Tag(name = "用户管理", description = "用户增删改查，以及资源授权管理")
@ApiVersion
public class SysUserController {
    /**
     * 服务对象
     */
    private final SysUserService sysUserService;

    /**
     * 新增用户信息
     *
     * @param request 新增用户信息请求实体
     * @return
     */
    @PostMapping
    @Operation(summary = "新增用户信息")
    public Result<SysUserResponse> save(@RequestBody @Validated SaveOrUpdateSysUserRequest request) {
        SysUser sysUser = SysUserConverter.INSTANCE.saveOrUpdateRequest2Entity(request);
        sysUserService.save(sysUser);
        return R.success(SysUserConverter.INSTANCE.entity2Response(sysUser));
    }


    /**
     * 更新用户信息
     *
     * @param request
     * @return
     */
    @PutMapping
    @Operation(summary = "更新用户信息")
    public Result<SysUserResponse> update(@RequestBody @Validated SaveOrUpdateSysUserRequest request) {
        SysUser sysUser = SysUserConverter.INSTANCE.saveOrUpdateRequest2Entity(request);
        sysUserService.updateById(sysUser);
        return R.success(SysUserConverter.INSTANCE.entity2Response(sysUser));
    }

    /**
     * 获取用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @Operation(summary = "获取用户详情")
    public Result<SysUserResponse> get(@Parameter(description = "用户ID") @PathVariable String id) {
        SysUser sysUser = sysUserService.getOptById(IdUtils.decode(id)).orElseThrow(() -> new BusinessException("获取用户信息失败"));
        return R.success(SysUserConverter.INSTANCE.entity2Response(sysUser));
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @GetMapping
    @Operation(summary = "分页查询")
    public Result<IPage<SysUserResponse>> page(@ParameterObject Page<SysUser> page) {
        IPage<SysUser> result = sysUserService.page(page);
        Page<SysUserResponse> sysUserResponsePage = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());
        List<SysUserResponse> list = result.getRecords().stream().map(SysUserConverter.INSTANCE::entity2Response).toList();
        sysUserResponsePage.setRecords(list);
        return R.success(sysUserResponsePage);
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @Operation(summary = "删除用户信息")
    public Result delete(@Parameter(description = "用户ID") @PathVariable String id) {
        sysUserService.removeById(IdUtils.decode(id));
        return R.success();
    }

}

